package com.ibigcat.module.mdm.controller.admin.org;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.CommonResult;
import com.ibigcat.framework.common.util.object.BeanUtils;
import static com.ibigcat.framework.common.pojo.CommonResult.success;

import com.ibigcat.framework.excel.core.util.ExcelUtils;

import com.ibigcat.framework.apilog.core.annotation.ApiAccessLog;
import static com.ibigcat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ibigcat.module.mdm.controller.admin.org.vo.*;
import com.ibigcat.module.mdm.dao.entity.OrgDO;
import com.ibigcat.module.mdm.service.OrgService;

@Tag(name = "管理后台 - 组织机构")
@RestController
@RequestMapping("/mdm/org")
@Validated
public class OrgController {

    @Resource
    private OrgService orgService;

    @PostMapping("/create")
    @Operation(summary = "创建组织机构")
    @PreAuthorize("@ss.hasPermission('mdm:org:create')")
    public CommonResult<Long> createOrg(@Valid @RequestBody OrgSaveReqVO createReqVO) {
        return success(orgService.createOrg(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织机构")
    @PreAuthorize("@ss.hasPermission('mdm:org:update')")
    public CommonResult<Boolean> updateOrg(@Valid @RequestBody OrgSaveReqVO updateReqVO) {
        orgService.updateOrg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织机构")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdm:org:delete')")
    public CommonResult<Boolean> deleteOrg(@RequestParam("id") Long id) {
        orgService.deleteOrg(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织机构")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdm:org:query')")
    public CommonResult<OrgRespVO> getOrg(@RequestParam("id") Long id) {
        OrgDO org = orgService.getOrg(id);
        return success(BeanUtils.toBean(org, OrgRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织机构分页")
    @PreAuthorize("@ss.hasPermission('mdm:org:query')")
    public CommonResult<PageResult<OrgRespVO>> getOrgPage(@Valid OrgPageReqVO pageReqVO) {
        PageResult<OrgDO> pageResult = orgService.getOrgPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrgRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织机构 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:org:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrgExcel(@Valid OrgPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrgDO> list = orgService.getOrgPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "组织机构.xls", "数据", OrgRespVO.class,
                        BeanUtils.toBean(list, OrgRespVO.class));
    }

}